﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SystemDirectory.Biz;
using SystemDirectory.Common;
using System.Data;
using SystemDirectory.Entity;

namespace SystemDirectory
{
    public partial class ProgrammAdd : System.Web.UI.Page
    {
        public static bool IsActiveTmp = false;
        public static bool IsCheck = false;
        public static Utilities.SearchType IsSearch;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                lbnModal.Attributes.Add("data-toggle", "modal");
                lbnModal.Attributes.Add("onclick", "btnModalClick()");


                cbxInactive.Checked = IsActiveTmp;
                BOContact buContact = new BOContact();
                DataTable dtContact = buContact.Get(txtFirstName.Text, txtSurName.Text, cbxInactive.Checked);


                PagedDataSource pds = new PagedDataSource();
                DataView dv = new DataView(dtContact);
                pds.DataSource = dv;
                pds.AllowPaging = true;
                pds.PageSize = 2;
                int NumPage = pds.PageCount;

                int CurrentPage;
                if (Request.QueryString["page"] != null)
                {
                    CurrentPage = Convert.ToInt32(Request.QueryString["page"].ToString());
                }
                else
                {
                    CurrentPage = 1;
                }

                pds.CurrentPageIndex = CurrentPage - 1;

                if (!pds.IsFirstPage)
                    hplBackward.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(CurrentPage - 1);
                else
                {

                    hplBackward.Visible = false;
                }
                if (!pds.IsLastPage)
                    hplForward.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(CurrentPage + 1);
                else
                {

                    hplForward.Visible = false;
                }


                rptContact.DataSource = pds;
                rptContact.DataBind();
                lbnEdit.Attributes.Add("style", "margin-top: 40px;margin-right: 10px");
                lblCurrent.Text = CurrentPage.ToString();
                lblTotal.Text = NumPage.ToString();

                //modalLookup.Visible = false;
                if (Request.QueryString["page"] != null)
                {
                    wrap.Visible = true;
                }
                else
                {
                    wrap.Visible = false;
                }

            }
            
        }

        protected void saveService_Click(object sender, EventArgs e)
        {
            Page.Validate("vgroup");
            if (Page.IsValid) { 
                Programme pg = new Programme();
                
                pg.ProgrammeName = ProgrammeName.Text;
                pg.Description = Description.Text;
                pg.ContactID = Convert.ToInt32(contact.Text);
                pg.IsActive = is_active.Checked;

                BOProgramme boProgramme = new BOProgramme();
                boProgramme.AddProgramme(pg);
                Response.Redirect("/Pages/Views/ProgrammeList.aspx");
            }

        }


        protected void cbxInactive_CheckedChanged(object sender, EventArgs e)
        {
            IsActiveTmp = cbxInactive.Checked;
            BOContact buContact = new BOContact();
            DataTable dtContact = buContact.Get(txtFirstName.Text, txtSurName.Text, cbxInactive.Checked);

            PagedDataSource pds = new PagedDataSource();
            DataView dv = new DataView(dtContact);
            pds.DataSource = dv;
            pds.AllowPaging = true;
            pds.PageSize = 2;
            int NumPage = pds.PageCount;

            int CurrentPage;
            if (Request.QueryString["page"] != null)
            {
                CurrentPage = Convert.ToInt32(Request.QueryString["page"].ToString());
            }
            else
            {
                CurrentPage = 1;
            }

            pds.CurrentPageIndex = CurrentPage - 1;

            if (!pds.IsFirstPage)
                hplBackward.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(CurrentPage - 1);
            else
            {

                hplBackward.Visible = false;
            }
            if (!pds.IsLastPage)
                hplForward.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(CurrentPage + 1);
            else
            {

                hplForward.Visible = false;
            }


            rptContact.DataSource = pds;
            rptContact.DataBind();
            lblCurrent.Text = CurrentPage.ToString();
            lblTotal.Text = NumPage.ToString();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BOContact buContact = new BOContact();
            DataTable dtContact = buContact.Get(txtFirstName.Text, txtSurName.Text, cbxInactive.Checked);
            PagedDataSource pds = new PagedDataSource();
            DataView dv = new DataView(dtContact);
            pds.DataSource = dv;
            pds.AllowPaging = true;
            pds.PageSize = 2;
            int NumPage = pds.PageCount;

            int CurrentPage;
            if (Request.QueryString["page"] != null)
            {
                CurrentPage = Convert.ToInt32(Request.QueryString["page"].ToString());
            }
            else
            {
                CurrentPage = 1;
            }

            pds.CurrentPageIndex = CurrentPage - 1;

            if (!pds.IsFirstPage)
                hplBackward.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(CurrentPage - 1);
            else
            {

                hplBackward.Visible = false;
            }
            if (!pds.IsLastPage)
                hplForward.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(CurrentPage + 1);
            else
            {

                hplForward.Visible = false;
            }

            rptContact.DataSource = pds;
            rptContact.DataBind();
            lblCurrent.Text = CurrentPage.ToString();
            lblTotal.Text = NumPage.ToString();
        }

        protected void lbnModal_Click(object sender, EventArgs e)
        {
            wrap.Visible = true;
        }

        protected void lbnEdit_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Pages/Forms/ContactEdit.aspx?id=" + txtId.Text);
        }
    }

}